package pl.put.dcs.workManagerServer.projectManager.queueManager.cacheManeger.CacheManagerImpl.jbossCacheManager;

import org.apache.log4j.Logger;
import org.jboss.cache.pojo.PojoCacheFactory;
import org.jboss.cache.pojo.notification.annotation.Attached;
import org.jboss.cache.pojo.notification.annotation.Detached;
import org.jboss.cache.pojo.notification.annotation.PojoCacheListener;
import org.jboss.cache.pojo.notification.event.AttachedEvent;
import org.jboss.cache.pojo.notification.event.DetachedEvent;
import pl.put.dcs.workManagerServer.projectManager.queueManager.cacheManeger.Cache;
import pl.put.dcs.workManagerServer.projectManager.queueManager.cacheManeger.CacheManagerImpl.CacheManagerImpl;
import pl.put.dcs.workManagerServer.projectManager.queueManager.cacheManeger.exceptions.CacheConnectionErrorException;
import pl.put.dcs.workManagerServer.projectManager.queueManager.cacheManeger.listeners.CacheContentChangeListener;
import pl.put.model.packet.Packet;

@PojoCacheListener
// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.4F55810B-75CB-50C7-61F5-3B699915DD21]
// </editor-fold> 
public class JBossCacheManager extends CacheManagerImpl
{
    public static Logger log = Logger.getLogger( JBossCacheManager.class.getName() );
    private org.jboss.cache.pojo.PojoCache cache = null;
    
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.6BF94735-402E-D3F9-8775-3A77B85E0CA9]
    // </editor-fold> 
    public JBossCacheManager( Cache cacheProp, String configFile )
    {
        super( cacheProp );
        log.info( "Start POJO Cache" );
        //String configFile = "jbosscacheConfig.xml";
        boolean toStart = false;
        cache = PojoCacheFactory.createCache( configFile, toStart );
        cache.addListener( this );
    }

    /**
     * Pakiet zostal usuniety z cacheu
     * @param event
     */
    @Detached
    public void packetIsDetached( DetachedEvent event )
    {
        Object p = event.getSource();
        // notyfikacja listenera
        if ( p instanceof Packet )
        {
            getCacheContentListener().cacheContentChanged( ( Packet )p, CacheContentChangeListener.ACTION.DETACHED );
        }
    }

    /**
     * Pakiet zostal wrzucony do cache'a
     * @param event
     */
    @Attached
    public void packetIsAttached( AttachedEvent event )
    {

        Object p = event.getSource();
        // notyfikacja listenera
        if ( p instanceof Packet )
        {
            getCacheContentListener().cacheContentChanged( ( Packet )p, CacheContentChangeListener.ACTION.ATACHED );
        }
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.A650C972-D90E-EB6B-2E97-635A56FEB0D8]
    // </editor-fold>
    @Override
    public void putPacketInCache( Packet packet )
    {
        try
        {
            if ( cache == null )
            {
                throw new CacheConnectionErrorException();
            }

            //Node rootNode = cache.getCache().getRoot();
            //Fqn reciverNode = Fqn.fromString( "/" + RECIVERS_NODE + "/" + packet.getReciverId() );
            log.debug( "Put pocket on cache. Packet id: " + packet.getId() );
            cache.attach( packet.getId().toString(), packet );
        }
        catch ( CacheConnectionErrorException ex )
        {
            log.error( ex.getMessage() );
            ex.printStackTrace();
        }

    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.661BBD03-B772-05A3-421C-CB5CC7D9F8A8]
    // </editor-fold> 
    @Override
    public boolean isPacketsNumberLimitReached()
    {
        return false;
    }

    /**
     * Usuwa z cache obiekt i go zwraca.
     * @param String packetId Id pakietu
     * @return
     */
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.F7226071-D071-80A6-1D4A-EF2BD41E808C]
    // </editor-fold> 
    @Override
    public Packet popPacketFromCache( String packetId )
    {
        log.debug( "Pop packet form cache. PacketId: " + packetId );
        return ( Packet )cache.detach( packetId );
    }

    @Override
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.5432356C-F869-07B6-D742-AEC43C780259]
    // </editor-fold> 
    public void connect()
    {
        cache.start();
    }

    @Override
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.DDB2F9BF-A185-5BC4-4A3F-4A85944C7A92]
    // </editor-fold> 
    public void setCacheContentChangeListener( CacheContentChangeListener val )
    {
        super.setCacheContentChangeListener( val );
    }

    @Override
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.C94D4002-B954-C42B-8575-890DA529FDAD]
    // </editor-fold> 
    public CacheContentChangeListener getCacheContentListener()
    {
        return super.getCacheContentListener();
    }

    @Override
    public Packet getPacketFromCache( String packetId )
    {
        return ( Packet )cache.find( packetId );
    }
}

